<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物业" prop="ohid">
        <el-select v-model="queryParams.ohid" placeholder="请选择所处物业" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in propertyapilist" :key="index" :label="item.propertyName" :value="item.pmid"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="费用类型" prop="ttid">
        <el-select v-model="queryParams.ttid" placeholder="请选择费用类型" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in ttapi" :key="index" :label="item.typeName" :value="item.ttid"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="ptid">
        <el-select v-model="queryParams.ptid" placeholder="请选择支付类型" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in ptapi" :key="index" :label="item.typeName" :value="item.ptid"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys-property-water:propertyWater:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sys-property-water:propertyWater:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="propertyWaterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="交易ID" align="center" prop="pwid" />
      <el-table-column label="物业名字" align="center" prop="payOrder.propertyManagement.propertyName" />
      <el-table-column label="订单编号" align="center" prop="payOrder.poid" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="实付金额" align="center" prop="orderPrice" />
      <el-table-column label="支付类型" align="center" prop="payOrder.payType.typeName" />
      <el-table-column label="费用类型" align="center" prop="payOrder.tradingType.typeName" />
      <el-table-column label="车牌号码" align="center" prop="payOrder.carsMsg.licenseNumber" />
      <el-table-column label="车主电话" align="center" prop="payOrder.carsMsg.carsTel" />
      <el-table-column label="是否分账" width="120" prop="pwStatus" >
        <template slot-scope="scope">
<!--          <el-tag  v-if="scope.row.pwStatus==0">未分账</el-tag>-->
<!--          <el-tag  v-if="scope.row.pwStatus==1">已分账</el-tag>-->
          {{ scope.row.pwStatus===0?'未分账':scope.row.pwStatus===1?'已分账':'状态有误' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['sys-property-water:propertyWater:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sys-property-water:propertyWater:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物业流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物业id" prop="ohid">
          <el-input v-model="form.ohid" placeholder="请输入物业id" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="实付金额" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入实付金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getttApiList, getptApiList, listPropertyWater, getPropertyWater, delPropertyWater, addPropertyWater, updatePropertyWater , getPropertyApi} from "@/api/modules/sys-property-water/propertyWater";

export default {
  name: "PropertyWater",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物业流水表格数据
      propertyWaterList: [],
      //物业集合模糊查询用
      propertyapilist:[],
      // 是否分账时间范围
      daterangeCreateTime: [],
      //物业集合模糊查询用
      ttapi:[],
      //物业集合模糊查询用
      ptapi:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        orderNum: null,
        orderPrice: null,
        pwStatus: null,
        ohid: null,
        ttid: parseInt(this.$route.query.ttid) ? parseInt(this.$route.query.ttid) : null,
        ptid: parseInt(this.$route.query.ptid) ? parseInt(this.$route.query.ptid) : null

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "唯一编码,前台使用不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        deleted: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        ohid: [
          { required: true, message: "物业id不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderPrice: [
          { required: true, message: "实付金额不能为空", trigger: "blur" }
        ],
        pwStatus: [
          { required: true, message: "是否分账不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物业流水列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPropertyWater(this.queryParams).then(response => {
        this.propertyWaterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getPropertyApi(this.queryParams).then(response=>{
        this.propertyapilist = response.rows;
      }).catch(err=>{
        alert("物业模块掉线")
      });
      getttApiList().then(response=>{
        this.ttapi = response.rows;
      });
      getptApiList().then(response=>{
        this.ptapi = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pwid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        ohid: null,
        orderNum: null,
        orderPrice: null,
        pwStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pwid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物业流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pwid = row.pwid || this.ids
      getPropertyWater(pwid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物业流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pwid != null) {
            updatePropertyWater(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPropertyWater(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pwids = row.pwid || this.ids;
      this.$modal.confirm('是否确认删除交易编号为"' + pwids + '"的数据项？').then(function() {
        return delPropertyWater(pwids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys-property/propertyWater/export', {
        ...this.queryParams
      }, `propertyWater_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
